﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Webadmin_Modules_Gallery_ListEdit : bzAdminUCBase
{
    public string _Act = "";
    public DataTable _dt = new DataTable();
    private bzGallery bz = new bzGallery();
    private string _ID = "";
    public DataRow dr = null;
    override protected void loadAction()
    {
        _Act = Request.QueryString["Act"] == null ? "" : Request.QueryString["Act"];
        _ID = Request.QueryString["ID"] == null ? "-1" : Request.QueryString["ID"];
        _dt = bz.getRecord(_ID, "-1");
        if (_dt.Rows.Count > 0)
        {
            dr = _dt.Rows[0];
        }
        if (!IsPostBack)
        {
            switch (_Act)
            {
                case "Edit": this.loadEdit(); break;
                case "Add": this.resetData(); break;
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initSiteLang(bzSiteOption.Group.Administrator, bzSiteOption.Note.Gallery, "Gallery");
    }
    private void loadEdit()
    {
        this.Navigator = this.AdminLang["MenuGallery"].ToString() + this.AdminLang["GeneralArrow"].ToString() + this.AdminLang["MenuGalleryList"].ToString() + this.AdminLang["GeneralArrow"].ToString() + this.AdminLang["Edit"].ToString();
        this.btnUpdate.Text = this.AdminLang["Edit"].ToString();
        _dt = bz.getRecord(_ID, "-1");
        DataRow dr = _dt.Rows[0];
        this.txtTitleVN.Text = dr["TitleVN"].ToString();

        this.txtDesVN.Text = Server.HtmlDecode(dr["DesVN"].ToString());
        this.txtContentVN.Text = Server.HtmlDecode(dr["ContentVN"].ToString());

        this.txtTitleEN.Text = dr["TitleEN"].ToString();

        this.txtDesEN.Text = Server.HtmlDecode(dr["DesEN"].ToString());
        this.txtContentEN.Text = Server.HtmlDecode(dr["ContentEN"].ToString());
        this.txtNoteVN.Text = dr["NoteVN"].ToString();
        this.txtNoteEN.Text = dr["NoteEN"].ToString();
        this.rdForward_1.Enabled = false;
        this.rdForward_0.Checked = true;

        this.rdEnabled_1.Checked = Convert.ToInt16(dr["Enabled"]) <= 0 ? false : true;
        this.rdEnabled_0.Checked = !this.rdEnabled_1.Checked;
    }
    private void resetData()
    {
        this.Navigator = this.AdminLang["MenuGallery"].ToString() + this.AdminLang["GeneralArrow"].ToString() + this.AdminLang["MenuGalleryList"].ToString() + this.AdminLang["GeneralArrow"].ToString() + this.AdminLang["AddNew"].ToString();
        this.txtTitleVN.Text = "";
        this.txtDesVN.Text = "";
        this.txtContentVN.Text = "";
        this.txtNoteVN.Text = "";
        this.txtTitleEN.Text = "";
        this.txtDesEN.Text = "";
        this.txtContentEN.Text = "";
        this.txtNoteEN.Text = "";
        this.rdForward_0.Checked = false;
        this.rdForward_1.Checked = !this.rdForward_0.Checked;
        this.btnUpdate.Text = this.AdminLang["AddNew"].ToString();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        switch (_Act)
        {
            case "Edit": this.doEdit(); break;
            case "Add": this.doAdd(); break;
        }
    }
    private void doEdit()
    {
        if (this.AllowEdit)
        {
            this.editImage();
        }
        else
        {
            Response.Redirect(bzStringClass.AlertPermission);
        }
    }
    private void doAdd()
    {
        if (this.AllowAdd)
        {
            this.insertImage();
        }
        else
        {
            Response.Redirect(bzStringClass.AlertPermission);
        }
    }
    //-----------------------------------------------------------------
    private void editImage()
    {
        DataTable dtTmp = bz.getRecord(_ID, "-1");
        if (dtTmp.Rows.Count > 0)
        {
            string sPath = Server.MapPath("~") + "/Images/Gallery/";

            string sNamePicMain_Thumb = dtTmp.Rows[0]["PicThumb"].ToString();
            string sNamePicMain_Full = dtTmp.Rows[0]["PicFull"].ToString();

            if (this.txtFile.HasFile)
            {
                string sPathSave = sPath + DateTime.Now.ToString("ddmmyyyy_hhmmss") + this.txtFile.FileName;
                while (bzFile.Exist(sPathSave))
                {
                    sPathSave = sPathSave.Replace(".", bzStringClass.ranNum(1) + ".");
                }
                this.txtFile.SaveAs(sPathSave);
                if (bz.insertImage(sPathSave, ref sNamePicMain_Thumb, ref sNamePicMain_Full))
                {
                    bzFile.delete(sPath + dtTmp.Rows[0]["PicThumb"]);
                    bzFile.delete(sPath + dtTmp.Rows[0]["PicFull"]);
                }
            }
            else
            {
                if (this.chkRemovePic.Checked)
                {
                    bzFile.delete(sPath + dtTmp.Rows[0]["PicThumb"]);
                    bzFile.delete(sPath + dtTmp.Rows[0]["PicFull"]);
                    sNamePicMain_Thumb = "";
                    sNamePicMain_Full = "";
                }

            }

            if (bz.updateRecord(_ID, this.txtTitleVN.Text, this.txtTitleEN.Text, Server.HtmlEncode(this.txtDesVN.Text), Server.HtmlEncode(this.txtDesEN.Text), Server.HtmlEncode(this.txtContentVN.Text), Server.HtmlEncode(this.txtContentEN.Text), sNamePicMain_Thumb, sNamePicMain_Full, this.txtNoteVN.Text, this.txtNoteEN.Text, this.rdEnabled_0.Checked ? "0" : "1", "0") > 0)
            {
                Response.Write(bzStringClass.addStringAlert(this.AdminLang["Success"]));
                Response.Redirect(Session["Page"].ToString());
            }
        }
    }
    private void insertImage()
    {
        string sPath = Server.MapPath("~") + "/Images/Gallery/";
        string sNamePicMain_Thumb = "";
        string sNamePicMain_Full = "";
        if (this.txtFile.HasFile)
        {
            string sPathSave = sPath + DateTime.Now.ToString("ddmmyyyy_hhmmss") + this.txtFile.FileName;
            while (bzFile.Exist(sPathSave))
            {
                sPathSave = sPathSave.Replace(".", bzStringClass.ranNum(1) + ".");
            }
            this.txtFile.SaveAs(sPathSave);
            bz.insertImage(sPathSave, ref sNamePicMain_Thumb, ref sNamePicMain_Full);
        }
        if (bz.insertRecord(this.txtTitleVN.Text, this.txtTitleEN.Text, Server.HtmlDecode(this.txtDesVN.Text), Server.HtmlDecode(this.txtDesEN.Text), Server.HtmlEncode(this.txtContentVN.Text), Server.HtmlEncode(this.txtContentEN.Text), sNamePicMain_Thumb, sNamePicMain_Full, this.txtNoteVN.Text, this.txtNoteEN.Text, this.rdEnabled_0.Checked ? "0" : "1", "0") > 0)
        {
            if (this.rdForward_0.Checked)
                Response.Redirect(Session["Page"].ToString());
            else this.resetData();
        }
    }
    //-------------------------------------------------------------------   
}
